package it.unibz.easytravel.blogging;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

public class BloggerAdapter {

	private String sessionId;

	public static final int RESULT_OK = 0;

	public static final int RESULT_AUTHENTICATION_FAILED = 1;
	
	public static final int RESULT_ERROR = 2;
	
	private static final String TAG = "EasyTravel BloggerAdapter";
	
	private static final String bloogFeedAddress = "http://www.blogger.com/feeds/default/blogs";
	
	private DefaultHttpClient httpClient;

	public BloggerAdapter() {
		httpClient = new DefaultHttpClient();
	}

	public int authenticate(String username, String password) throws Exception {
		

		HttpPost httpPost = new HttpPost("https://www.google.com/accounts/ClientLogin?" +
              "Email=" + username +
              "&Passwd=" + password +              
              "&service=blogger&" +
              "accountType=BLOGGER&" +
              "source=AndroidApplication");
		
		httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 		
		HttpResponse response = httpClient.execute(httpPost);
		StatusLine statusLine = response.getStatusLine();
		
		int code = statusLine.getStatusCode();
		if (code == HttpURLConnection.HTTP_OK) {
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String bodyString = responseHandler.handleResponse(response);				
			this.sessionId = ResponseParser.parseSessionId(bodyString);
			if (sessionId == null)
				return RESULT_ERROR;
			Log.i(TAG, "Authentication successful, session id: " + sessionId);
			
			return RESULT_OK;
		} else if (code == HttpURLConnection.HTTP_FORBIDDEN) {
			Log.i (TAG, "Authentification failed, username or password is wrong");
			return RESULT_AUTHENTICATION_FAILED;
		}	
		else {
			return RESULT_ERROR;
		}
	}
	
	//It takes the id of the FIRST blog from possibly many for now
	public String retrieveBlogId () throws ClientProtocolException, IOException {
		
		
		HttpGet httpGet = new HttpGet(bloogFeedAddress);
		
		httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
		httpGet.setHeader("GData-Version", "2");
		String authHeaderValue = "GoogleLogin auth=" + sessionId;
		httpGet.setHeader("Authorization", authHeaderValue);
		
		
		HttpResponse response = httpClient.execute(httpGet);
		StatusLine statusLine = response.getStatusLine();
		int code = statusLine.getStatusCode();
		if (code == HttpURLConnection.HTTP_OK) {
			Log.d (TAG, "Feed HTTP OK");
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String atomXmlEntryString = responseHandler.handleResponse(response);
			Log.d(TAG, atomXmlEntryString);
		}
		else {
			Log.d (TAG, "Feed HTTP BAD " + code);		
		}
				
		
		
		return "";
	} 

}
